package cn.shujuhai.workflow.service.impl;

import cn.shujuhai.common.pluging.data.ResultVo;
import cn.shujuhai.workflow.domain.FlowDefinition;
import cn.shujuhai.workflow.mapper.FlowDefinitionMapper;
import cn.shujuhai.workflow.service.FlowDefinitionService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

/**
 * @author zdz18
 * @description 针对表【flow_definition】的数据库操作Service实现
 * @createDate 2025-04-01 10:05:55
 */
@Service
public class FlowDefinitionServiceImpl extends ServiceImpl<FlowDefinitionMapper, FlowDefinition>
        implements FlowDefinitionService {


    public ResultVo<?> flowDefinitionList(int pageNum, int pageSize, String search) {
        PageHelper.startPage(pageNum, pageSize);
        List<FlowDefinition> flowDefinitionList = this.list(Wrappers.<FlowDefinition>lambdaQuery()
                .like(FlowDefinition::getName, search)
                .orderByDesc(FlowDefinition::getId));
        PageInfo<FlowDefinition> pageInfo = new PageInfo<>(flowDefinitionList);
        return ResultVo.success(pageInfo);

    }

    public ResultVo<?> saveFlowDefinition(Map<String, String> event) {
        String key = event.get("id");
        String name = event.get("name");
        String xml = event.get("xml");
        FlowDefinition flowDefinition = new FlowDefinition();
        flowDefinition.setName(name);
        flowDefinition.setFlowKey(key);
        flowDefinition.setBpmnXml(xml.getBytes(StandardCharsets.UTF_8));
        boolean save = this.save(flowDefinition);
        if (save) {
            return ResultVo.success();
        }
        return ResultVo.failure("保存失败");
    }

    @Override
    public ResultVo<?> updateFlowDefinition(Map<String, String> event) {
        Long flowDefinitionId = Long.valueOf(event.get("flowDefinitionId"));
        String key = event.get("id");
        String name = event.get("name");
        String xml = event.get("xml");
        FlowDefinition flowDefinition = new FlowDefinition();
        flowDefinition.setId(flowDefinitionId);
        flowDefinition.setName(name);
        flowDefinition.setFlowKey(key);
        flowDefinition.setBpmnXml(xml.getBytes(StandardCharsets.UTF_8));
        boolean save = this.updateById(flowDefinition);
        if (save) {
            return ResultVo.success();
        }
        return ResultVo.failure("更新失败");
    }


}




